﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.IO;

namespace DataAccessObject
{
    public static class StaticClass
    {
        public static bool CheckAll(GridView gv)
        {
            bool returnvalue = true;
            CheckBox cb = new CheckBox();
            CheckBox ch = new CheckBox();
            ch = ((CheckBox)gv.HeaderRow.FindControl("cbHeader"));
            for (int i = 0; i < gv.Rows.Count; i++)
            {
                cb = ((CheckBox)gv.Rows[i].FindControl("cbDetail"));
                if (!cb.Checked)
                {
                    returnvalue = false;
                    break;
                }
            }
            return returnvalue;
        }
        public static bool CheckHeader(GridView gv)
        {
            bool returnvalue = true;
            CheckBox ch = new CheckBox();
            ch = ((CheckBox)gv.HeaderRow.FindControl("cbHeader"));
            if (!ch.Checked)
            {
                returnvalue = false;
            }
            return returnvalue;
        }
        public static void UploadData(ref System.Web.UI.WebControls.Image img,
          string sSavePath,
          FileUpload filUpload,
          ref string Thump_Name_File)
        {

            // Initialize variables
            string sThumbExtension;
            int intThumbWidth;
            int intThumbHeight;
            // Set constant values
            sThumbExtension = "_thumb";
            intThumbWidth = 160;
            intThumbHeight = 120;
            // If file field isn�t empty
            if (filUpload.PostedFile != null)
            {
                // Check file size (mustn�t be 0)
                HttpPostedFile myFile = filUpload.PostedFile;
                int nFileLen = myFile.ContentLength;
                if (nFileLen == 0)
                {
                    // "No file was uploaded.";
                    return;
                }
                // Check file extension (must be JPG)
                if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
                {
                    // "The file must have an extension of JPG";
                    return;
                }
                // Read file into a data stream
                byte[] myData = new Byte[nFileLen];
                myFile.InputStream.Read(myData, 0, nFileLen);
                // Make sure a duplicate file doesn�t exist.  If it does, keep on appending an 
                // incremental numeric until it is unique
                string sFilename = System.IO.Path.GetFileName(myFile.FileName);
                int file_append = 0;
                while (System.IO.File.Exists(HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
                {
                    file_append++;
                    sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
                                     + file_append.ToString() + ".jpg";
                }
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(sSavePath)))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(sSavePath));
                    // Save the stream to disk
                    System.IO.FileStream newFile
                            = new System.IO.FileStream(HttpContext.Current.Server.MapPath(sSavePath + sFilename),
                                                       System.IO.FileMode.Create);
                    newFile.Write(myData, 0, myData.Length);
                    newFile.Close();
                }
                else
                {
                    System.IO.FileStream newFile
                                               = new System.IO.FileStream(HttpContext.Current.Server.MapPath(sSavePath + sFilename),
                                                                          System.IO.FileMode.Create);
                    newFile.Write(myData, 0, myData.Length);
                    newFile.Close();
                }
                // Check whether the file is really a JPEG by opening it

                System.Drawing.Image.GetThumbnailImageAbort myCallBack =
                               new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap myBitmap;
                try
                {
                    myBitmap = new Bitmap(HttpContext.Current.Server.MapPath(sSavePath + sFilename));
                    // If jpg file is a jpeg, create a thumbnail filename that is unique.

                    file_append = 0;
                    string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
                                                             + sThumbExtension + ".jpg";
                    while (System.IO.File.Exists(HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
                    {
                        file_append++;
                        sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
                                       file_append.ToString() + sThumbExtension + ".jpg";
                    }
                    // Save thumbnail and output it onto the webpage
                    System.Drawing.Image myThumbnail
                            = myBitmap.GetThumbnailImage(intThumbWidth,
                                                         intThumbHeight, myCallBack, IntPtr.Zero);
                    myThumbnail.Save(HttpContext.Current.Server.MapPath(sSavePath + sThumbFile));
                    img.ImageUrl = sSavePath + sThumbFile;
                    Thump_Name_File = sThumbFile;
                    // Displaying success information
                    // "File uploaded successfully!";
                    // Destroy objects
                    myThumbnail.Dispose();
                    myBitmap.Dispose();
                }
                catch (ArgumentException errArgument)
                {
                    // "The file wasn't a valid jpg file.";
                    System.IO.File.Delete(HttpContext.Current.Server.MapPath(sSavePath + sFilename));
                }

            }
        }
        public static void DeleteTemp()
        {
            string[] filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Club_ColorImages/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Club_Images/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Club_Logos/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Coach_Images/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/League_Logos/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/NationalTeam_ColorImages/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/NationalTeam_Images/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/NationalTeam_Logos/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Player_Images/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);
            filePaths = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/Stadium_Images/"));
            foreach (string filePath in filePaths)
                File.Delete(filePath);

        }
        public static bool ThumbnailCallback()
        {
            return false;
        }    
    }
}
